package com.ruochen.common.utils;

/**
 * Created by 宋俊 on 2017/12/20.
 */

public class NoDoubleClickUtils {
    private static long lastClickTime = 0;
    private final static int SPACE_TIME = 500;
    private static int lastButtonId = -1;

    public synchronized static boolean isDoubleClick() {
        long currentTime = System.currentTimeMillis();
        boolean isClick2;
        if (currentTime - lastClickTime >
                SPACE_TIME) {
            isClick2 = false;
        } else {
            isClick2 = true;
        }
        lastClickTime = currentTime;
        return isClick2;
    }

    /**
     * 判断两次点击的间隔，
     * 如果小于设置时间，
     * 则认为是多次无效点击
     *
     * @return
     */
    public synchronized static boolean isFastDoubleClick() {
        return isFastDoubleClick(-1, SPACE_TIME);
    }

    /**
     * 判断同一个控件两次点击的间隔，
     * 如果小于置时间，
     * 则认为是多次无效点击
     *
     * @return
     */
    public synchronized static boolean isFastDoubleClick(int buttonId) {
        return isFastDoubleClick(buttonId, SPACE_TIME);
    }

    /**
     * 判断两次点击的间隔，
     * 如果小于diff，
     * 则认为是多次无效点击
     *
     * @param diff
     * @return
     */
    public synchronized static boolean isFastDoubleClick(int buttonId, long diff) {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (lastButtonId == buttonId && lastClickTime > 0 && timeD < diff) {
            return false;
        }
        lastClickTime = time;
        lastButtonId = buttonId;
        return true;
    }
}
